package pers.lan.array;

import java.util.Arrays;

/**
 * @author <p><a href="mailto:1728209643@qq.com">XiaoSan</a>
 * @date 2019/11/22 2:32 PM
 */
public class LC_922 {

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new LC_922().sortArrayByParityII(new int[]{
                4, 2, 5, 7
        })));
    }

    public int[] sortArrayByParityII(int[] A) {
        int i = 0, j = 1;
        for (; i < A.length; i += 2) {
            if (A[i] % 2 != 0) {
                while (A[j] % 2 != 0) {
                    j += 2;
                }
                swap(i, j, A);
            }
        }
        return A;
    }

    private void swap(int i, int j, int[] a) {
        int t = a[i];
        a[i] = a[j];
        a[j] = t;
    }
}
